package com.supermap.business.cloud.assets;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.assets.FlieDocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author aqua
 * @description
 * @date 2019年10月28 17:51
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/assets/file")
@CrossOrigin
@Slf4j
public class FlieDocController
{

    public static final String separator = File.separator;
    @Reference(timeout = 10000)
    FlieDocService flieDocService;



    @GetMapping("/mapGetArea")
    @ResponseBody
    public JSONUtils mapGetArea(String areaCode){
        return flieDocService.mapGetArea(areaCode);
    }

    @GetMapping("/getFileDocPageList")
    @ResponseBody
    public JSONUtils getFileDocPageList(String areaCode,Integer page,Integer size){
        return flieDocService.getFileDoc(areaCode,page,size);
    }

    @PutMapping("/insert")
    @ResponseBody
    public JSONUtils insert(String fileName,String title,String userId,String areaCode){
        return  flieDocService.insert(fileName,title,userId,areaCode);
    }

    @DeleteMapping("/delFileDoc")
    @ResponseBody
    public JSONUtils delFileDoc(Integer id,String userId){
        return  flieDocService.delFileDoc(id,userId);
    }

    @ResponseBody
    @RequestMapping(value= "download")
    public String download(HttpServletRequest request,String path, HttpServletResponse response){
        File fileurl = new File(path);
        //浏览器下载后的文件名称showValue,从url中截取到源文件名称以及，以及文件类型，如board.docx;
        String showValue = path.substring(path.lastIndexOf("/")+1);
        System.out.println(showValue);
        try{
            //根据条件得到文件路径
            System.out.println("===========文件路径==========="+fileurl);
            //将文件读入文件流
            InputStream inStream = new FileInputStream(fileurl);
            //获得浏览器代理信息
            final String userAgent = request.getHeader("USER-AGENT");
            //判断浏览器代理并分别设置响应给浏览器的编码格式
            String finalFileName = null;
            if(userAgent.equals("MSIE")||userAgent.equals("Trident")){//IE浏览器
                finalFileName = URLEncoder.encode(showValue,"UTF8");
                System.out.println("IE浏览器");
            }else if(userAgent.equals("Mozilla")){//google,火狐浏览器
                finalFileName = new String(showValue.getBytes(), "ISO8859-1");
            }else{
                finalFileName = URLEncoder.encode(showValue,"UTF8");//其他浏览器
            }
            //设置HTTP响应头
            response.reset();//重置 响应头
            response.setContentType("application/x-download");//告知浏览器下载文件，而不是直接打开，浏览器默认为打开
            response.addHeader("Content-Disposition" ,"attachment;filename=\"" +finalFileName+ "\"");//下载文件的名称

            // 循环取出流中的数据
            byte[] b = new byte[1024];
            int len;
            while ((len = inStream.read(b)) > 0){
                response.getOutputStream().write(b, 0, len);
            }
            inStream.close();
            response.getOutputStream().close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }



}
